package com.ruoyi.jiajia.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.JiajiaMessage;
import com.ruoyi.system.domain.JiajiaUser;
import com.ruoyi.system.domain.JiajiaUserCoupon;
import com.ruoyi.system.service.IJiajiaMessageService;
import com.ruoyi.system.service.IJiajiaUserCouponService;
import com.ruoyi.system.service.IJiajiaUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JiajiaCoupon;
import com.ruoyi.system.service.IJiajiaCouponService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author knd
 * @date 2021-10-14
 */
@Controller
@RequestMapping("/jiajia/coupon")
public class JiajiaCouponController extends BaseController
{
    private String prefix = "jiajia/coupon";

    @Autowired
    private IJiajiaCouponService jiajiaCouponService;

    @Autowired
    private IJiajiaUserService userService;

    @Autowired
    private IJiajiaUserCouponService userCouponService;

    @Autowired
    private IJiajiaMessageService messageService;

    @RequiresPermissions("jiajia:coupon:view")
    @GetMapping()
    public String coupon()
    {
        return prefix + "/coupon";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("jiajia:coupon:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaCoupon jiajiaCoupon)
    {
        startPage();
        List<JiajiaCoupon> list = jiajiaCouponService.selectJiajiaCouponList(jiajiaCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("jiajia:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaCoupon jiajiaCoupon)
    {
        List<JiajiaCoupon> list = jiajiaCouponService.selectJiajiaCouponList(jiajiaCoupon);
        ExcelUtil<JiajiaCoupon> util = new ExcelUtil<JiajiaCoupon>(JiajiaCoupon.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("jiajia:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaCoupon jiajiaCoupon)
    {
        if (jiajiaCoupon.getSxDay()==null||jiajiaCoupon.getSxDay().intValue()<=0){
            return AjaxResult.error("优惠券天数不能为零");
        }
        return toAjax(jiajiaCouponService.insertJiajiaCoupon(jiajiaCoupon));
    }

    /**
     * 修改优惠券
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaCoupon jiajiaCoupon = jiajiaCouponService.selectJiajiaCouponById(id);
        mmap.put("jiajiaCoupon", jiajiaCoupon);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("jiajia:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaCoupon jiajiaCoupon)
    {
        if (jiajiaCoupon.getSxDay()==null||jiajiaCoupon.getSxDay().intValue()<=0){
            return AjaxResult.error("优惠券天数不能为零");
        }
        return toAjax(jiajiaCouponService.updateJiajiaCoupon(jiajiaCoupon));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("jiajia:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaCouponService.deleteJiajiaCouponByIds(ids));
    }


    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("jiajia:coupon:edit")
    @PostMapping("/faFang")
    @ResponseBody
    public AjaxResult faFang(JiajiaCoupon search)
    {
        if (search.getId().intValue() <= 0){
            return AjaxResult.refreshAgain();
        }
        JiajiaCoupon coupon = jiajiaCouponService.selectJiajiaCouponById(search.getId());

        List<JiajiaUser> userList = userService.selectJiajiaUserList(null);

        for (JiajiaUser model : userList) {
            JiajiaUserCoupon userCoupon = new JiajiaUserCoupon();
            userCoupon.setUid(model.getId());
            userCoupon.setCid(coupon.getId());
            userCoupon.setSxTime(DateUtils.getDayBeforOrAfterByToDay(coupon.getSxDay()));
            userCouponService.insertJiajiaUserCoupon(userCoupon);

            JiajiaMessage message = new JiajiaMessage();
            message.setType(2);
            message.setTitle("优惠券到账通知");
            message.setUid(model.getId());
            message.setContent("系统送您一张价值"+coupon.getPrice()+"元的"+coupon.getName()+",请尽快使用!");
            messageService.insertJiajiaMessage(message);
        }


        return AjaxResult.success("发放成功");
    }


}
